package com.financing.platform.web.api;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.BizEnums.BiddingReceiptEnums;
import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.FpBiddingReceiptDTO;
import com.financing.platform.service.IFpBiddingReceiptService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.vo.FpBiddingReceiptVO;

@Controller("fpBiddingReceiptAction")
@RequestMapping("/api/fpBiddingReceipt")
@Scope("prototype")
public class FpBiddingReceiptAction extends BaseAction {

	@Autowired
	IFpBiddingReceiptService fpBiddingReceiptService;

	/***
	 * 
	
	* @Title: queryFpBiddingReceipt
	
	* @Description: 查询竞价
	
	* @param @param request
	* @param @param response
	* @param @param dto
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping("/queryFpBiddingReceipt.shtml")
	@ResponseBody
	public ResultMap queryFpBiddingReceipt(HttpServletRequest request,
			HttpServletResponse response, FpBiddingReceiptDTO dto,Pager page) {
		ResultMap resultMap = new ResultMap();
		try {
			dto.setBegin(page.getStartIndex());
			dto.setEnd(page.getPageSize());
			if(StringUtils.isNotBlank(dto.getStartBiddingDate())){
				String s=dto.getStartBiddingDate();
				dto.setStartBiddingDate(s+" 00:00:00");
				if(StringUtils.isBlank(dto.getEndBiddingDate())){
					dto.setEndBiddingDate(s+" 23:59:59");
				}
			}
			dto.setFlagBail(BiddingReceiptEnums.FLAG_BAIL_YES);//只查出已交保证金
			int countForegroundProduct = fpBiddingReceiptService.queryFpBiddingReceiptCount(dto);
			page.setTotalCount(countForegroundProduct);
			//查询竞价列表
			List<FpBiddingReceiptVO> queryFpBiddingReceipt = fpBiddingReceiptService.queryFpBiddingReceipt(dto);
			resultMap.setAttribute("fpBiddingReceiptList",queryFpBiddingReceipt);
			resultMap.setAttribute("pager", page);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("query queryFpBiddingReceipt info error!", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	/***
	 * 
	
	* @Title: queryFpBiddingReceiptDetail
	
	* @Description: 查询竞价详情
	
	* @param @param request
	* @param @param response
	* @param @param dto
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping("/queryFpBiddingReceiptDetail.shtml")
	@ResponseBody
	public ResultMap queryFpBiddingReceiptDetail(HttpServletRequest request,
			HttpServletResponse response, FpBiddingReceiptDTO dto) {
		ResultMap resultMap = new ResultMap();
		try {
			int id=dto.getId();
			if(id==0){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			Map<String, Object> queryFpBiddingReceiptDetail = fpBiddingReceiptService.queryFpBiddingReceiptDetail(dto);
			
			if(queryFpBiddingReceiptDetail==null||queryFpBiddingReceiptDetail.isEmpty()){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_RESULTDATA_NOTEXIST);
				return resultMap;
			}
			
			resultMap.setAttribute("fpBiddingReceiptDetail",queryFpBiddingReceiptDetail);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("query fpBiddingReceiptDetail info error!", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}

	
	/***
	 * 
	
	* @Title: checkBiddingStatus
	
	* @Description: 审核竞价
	
	* @param @param request
	* @param @param response
	* @param @param dto
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping("/checkBiddingStatus.shtml")
	@ResponseBody
	public ResultMap checkBiddingStatus(HttpServletRequest request,
			HttpServletResponse response, FpBiddingReceiptDTO dto) {
		ResultMap resultMap = new ResultMap();
		try {
			String checkBiddingStatus = dto.getBiddingStatus();
			if(dto.getId()==0||StringUtils.isBlank(checkBiddingStatus)){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			//审核失败需要填写原因    6 收回承销权原因   和     审核失败  原因
			boolean flagFailStatus=BiddingReceiptEnums.BIDDING_STATUS_SB.equalsIgnoreCase(checkBiddingStatus)
					||BiddingReceiptEnums.BIDDING_STATUS_SH.equalsIgnoreCase(checkBiddingStatus);
			if(flagFailStatus&&StringUtils.isBlank(dto.getFailReson())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}else if(!flagFailStatus){
				//除了2和6 原因都为空
				dto.setFailReson("");
			}
			
			Map<String, Object> currentBidding = fpBiddingReceiptService.queryFpBiddingReceiptDetail(dto);			
			
			if(currentBidding==null||currentBidding.get("loginName")==null){
				//不存在，    竞价
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_RESULTDATA_NOTEXIST);
				return resultMap;
			}
			
			String biddingStatus=(String) currentBidding.get("biddingStatus");
			if(Integer.parseInt(biddingStatus)>=Integer.parseInt(checkBiddingStatus)){
				//状态不能逆转   不能相同
				//不存在，    竞价
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_NOT_BACK);
				return resultMap;
			}
			
			//是否交了保证金   才能审核通过
			String flagBail=(String) currentBidding.get("flagBail");
			//是否交保证金
			boolean flagBailBoolean=StringUtils.isBlank(flagBail)||BiddingReceiptEnums.FLAG_BAIL_NO.equals(flagBail);
			//审核成功判断是否有交保证金
			if(BiddingReceiptEnums.BIDDING_STATUS_CG.equalsIgnoreCase(checkBiddingStatus)&&flagBailBoolean){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_NOT_BAIL_AMOUNT);
				return resultMap;
			}
			
			DTOdateSet.setUpdateInfo(dto, request.getSession());
			//修改审核状态
			fpBiddingReceiptService.updateBiddingReceipt(dto,currentBidding);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("query checkBiddingStatus info error!", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}

}
